import { md5 } from 'js-md5'
 
//  请求头
export function isHeaders(flg?:boolean) {
  const key = 'Hnszwhg'
  const secret = 'hNsZwhg@2019'
  const timeStamp = new Date().getTime()
  let host = ''
  const parms= {
    'timestamp' : timeStamp,
  }
 
  // flg =true,  平台接口
  if(!flg){ 
    if (import.meta.env.NODE_ENV !== 'production') {
      host = 'www.hnszwhg.cn'
    } else {
      host = window.location.hostname
    }
  }else{
    parms['client-type']='browser'
  }
  parms['sign']= md5(key + timeStamp + secret + host) 
  return  parms
}

/**
 * custom base64
 * 解密
 */
export function decryptCustomBase64(cusotmStr, start_site, str_length) {
    // 1、掐头去尾
    const strArr = [0, 0, 4, 19]
    start_site = (start_site === undefined ? strArr[2] : start_site)
    str_length = (str_length === undefined ? strArr[3] : str_length)
    cusotmStr = cusotmStr.substring(0, cusotmStr.length - str_length)
    cusotmStr = cusotmStr.substring(str_length, cusotmStr.length)
        // 2、去中间
    const t = start_site * 1 + str_length * 1
    const str = cusotmStr.substring(start_site, t)
    const st = cusotmStr.split(str).join('')
        // console.log(st)
    return atob(st)
}

/**
* custom base64
* 加密
*/
export function ecryptCustomBase64(str, start_site, str_length) {
    const strArr = [0, 0, 4, 19]
    start_site = (start_site === undefined ? strArr[2] : start_site)
    str_length = (str_length === undefined ? strArr[3] : str_length)
    let base64 =btoa(str)
    start_site = start_site * 1
    str_length = str_length * 1
    base64 = base64.slice(0, start_site) + randomString(str_length,null) + base64.slice(start_site)
    return randomString(str_length,null) + base64 + randomString(str_length,null)
}
function randomString(len, charSet) {
    charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
    let randomString = ''
    for (let i = 0; i < len; i++) {
        const randomPoz = Math.floor(Math.random() * charSet.length)
        randomString += charSet.substring(randomPoz, randomPoz + 1)
    }
    return randomString
}